/**
 * Copyright &copy; com.nmf
 */
package com.nmf.site.biz.applyperiod.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.nmf.site.common.config.Global;
import com.nmf.site.common.excep.SiteException;
import com.nmf.site.common.persistence.Page;
import com.nmf.site.common.web.BaseController;
import com.nmf.site.common.utils.StringUtils;
import com.nmf.dao.gen.bean.BizApplyPeriod;
import com.nmf.dao.gen.BizApplyPeriodMapper;

import com.nmf.site.biz.applyperiod.service.BizApplyPeriodService;

/**
 * 申请周期Controller
 * @author bm
 * @version 2019-02-13
 */
@Controller
@RequestMapping(value = "${adminPath}/applyperiod/bizApplyPeriod")
public class BizApplyPeriodController extends BaseController {

	@Autowired
	private BizApplyPeriodService bizApplyPeriodService;
	
	@ModelAttribute
	public BizApplyPeriod get(@RequestParam(required=false) String id,@RequestParam(required=false) String opt,HttpServletRequest request) {
		BizApplyPeriod entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = bizApplyPeriodService.get(id);
		}
		if (StringUtils.isNotBlank(opt)){
			request.setAttribute("opt", opt);
		}
		if (entity == null){
			entity = new BizApplyPeriod();
		}
		return entity;
	}
	
	@RequiresPermissions("applyperiod:bizApplyPeriod:view")
	@RequestMapping(value = {"list", ""})
	public String list(BizApplyPeriod bizApplyPeriod, HttpServletRequest request, HttpServletResponse response, Model model) {
		try{
			Page<BizApplyPeriod> page = bizApplyPeriodService.findPage(new Page<BizApplyPeriod>(request, response), bizApplyPeriod); 
			model.addAttribute("page", page);
		}catch(SiteException e){
			model.addAttribute("message", e.getMessage());
			return "biz/applyperiod/bizApplyPeriodList";
		}
		return "biz/applyperiod/bizApplyPeriodList";
	}
	
	@RequiresPermissions("applyperiod:bizApplyPeriod:view")
	@RequestMapping(value = "form")
	public String form(BizApplyPeriod bizApplyPeriod, Model model) {
		model.addAttribute("bizApplyPeriod", bizApplyPeriod);
		return "biz/applyperiod/bizApplyPeriodForm";
	}

	@RequiresPermissions("applyperiod:bizApplyPeriod:edit")
	@RequestMapping(value = "save")
	public String save(BizApplyPeriod bizApplyPeriod, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, bizApplyPeriod)){
			return form(bizApplyPeriod, model);
		}
		try{
			bizApplyPeriodService.save(bizApplyPeriod);
		}catch(SiteException e){
			addMessage(redirectAttributes, e.getMessage());
			return "redirect:"+Global.getAdminPath()+"/applyperiod/bizApplyPeriod/?repage";
		}
		addMessage(redirectAttributes, "保存申请周期成功");
		return "redirect:"+Global.getAdminPath()+"/applyperiod/bizApplyPeriod/?repage";
	}
	
	@RequiresPermissions("applyperiod:bizApplyPeriod:edit")
	@RequestMapping(value = "delete")
	public String delete(BizApplyPeriod bizApplyPeriod, RedirectAttributes redirectAttributes) {
		try{
			bizApplyPeriodService.delete(bizApplyPeriod);
		}catch(SiteException e){
			addMessage(redirectAttributes, e.getMessage());
			return "redirect:"+Global.getAdminPath()+"/applyperiod/bizApplyPeriod/?repage";
		}
		addMessage(redirectAttributes, "删除申请周期成功");
		return "redirect:"+Global.getAdminPath()+"/applyperiod/bizApplyPeriod/?repage";
	}

}